package com.chengliang.bbs.controller;

import com.chengliang.bbs.entry.JSONResult;
import com.chengliang.bbs.entry.UserAuths;
import com.chengliang.bbs.service.MessageService;
import com.chengliang.bbs.util.UserUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 消息模块
 * @author chengliang
 */
@Slf4j
@Controller
@RequestMapping("/message")
public class MessageController {

    @Autowired
    private MessageService messageService;

    /**
     * 删除所有消息
     * @return
     */
    @PostMapping("/remove/all")
    @ResponseBody
    public JSONResult removeAll(){
        return JSONResult.is(messageService.removeMessageAll(UserUtils.currentUser().getUserId()),"清空消息");
    }


    /**
     * 删除消息
     * @param id
     * @return
     */
    @PostMapping("/remove")
    @ResponseBody
    public JSONResult remove(Integer id){
       return JSONResult.is(messageService.removeMessageBy(UserUtils.currentUser().getUserId(),id),"消息删除");
    }

    /**
     * 将消息设置为已读
     * @return
     */
    @PostMapping("/read")
    @ResponseBody
    public JSONResult read(){
        return JSONResult.is(messageService.read(UserUtils.currentUser().getUser().getUserId()),"消息读取");
    }


    /**
     * 当前用户消息数量
     * @return
     */
    @PostMapping("/nums")
    @ResponseBody
    public JSONResult nums(){
        UserAuths userAuths = UserUtils.currentUser();
        if(userAuths == null){
            return JSONResult.success().append("count",0);
        }
        log.info("消息数量，当前用户信息{}",userAuths.getUser());
        return JSONResult.success().append("count",messageService.getMessageNumber(userAuths.getUser().getUserId()));
    }


}
